home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 198_02 / spawn.c < prev    next >
C/C++ Source or Header  |  1990-01-21  |  20KB  |  748 lines

  1. /*    Spawn:    various DOS access commands
  2.         for MicroEMACS
  3. */
  4.  
  5. #include        <stdio.h>
  6. #include    "estruct.h"
  7. #include        "edef.h"
  8.  
  9. #if     AMIGA
  10. #define  NEW   1006L
  11. #endif
  12.  
  13. #if        ST520 & MEGAMAX
  14. #include <osbind.h>
  15. #include <string.h>
  16. #define LOAD_EXEC 0     /* load and execute the program */
  17. char    *STcmd,        /* the command filename & path  */
  18.     *STargs,    /* command args (if any)        */
  19.     *STenv,        /* environment                  */
  20.     *STwork;    /* work area            */
  21. #endif
  22.  
  23. #if     VMS
  24. #define EFN     0                               /* Event flag.          */
  25.  
  26. #include        <ssdef.h>                       /* Random headers.      */
  27. #include        <stsdef.h>
  28. #include        <descrip.h>
  29. #include        <iodef.h>
  30.  
  31. extern  int     oldmode[3];                     /* In "termio.c"        */
  32. extern  int     newmode[3];                     /* In "termio.c"        */
  33. extern  short   iochan;                         /* In "termio.c"        */
  34. #endif
  35.  
  36. #if     V7 | USG | BSD
  37. #include        <signal.h>
  38. #endif
  39.  
  40. extern int vttidy();
  41.  
  42. #if    MSDOS & (MSC | TURBO)
  43. #include    <process.h>
  44. #endif
  45.  
  46. /*
  47.  * Create a subjob with a copy of the command intrepreter in it. When the
  48.  * command interpreter exits, mark the screen as garbage so that you do a full
  49.  * repaint. Bound to "^X C". The message at the start in VMS puts out a newline.
  50.  * Under some (unknown) condition, you don't get one free when DCL starts up.
  51.  */
  52. spawncli(f, n)
  53. {
  54. #if     AMIGA
  55.         long newcli;
  56.  
  57. #endif
  58.  
  59. #if     V7 | USG | BSD
  60.         register char *cp;
  61.         char    *getenv();
  62. #endif
  63.  
  64.     /* don't allow this command if restricted */
  65.     if (restflag)
  66.         return(resterr());
  67.  
  68. #if    AMIGA
  69.         mlwrite("[Starting new CLI]");
  70.         sgarbf = TRUE;
  71.         Execute("NEWCLI \"CON:0/0/640/200/MicroEMACS Subprocess\"", 0L, 0L);
  72.         return(TRUE);
  73. #endif
  74.  
  75. #if     VMS
  76.     ttscroll(0, term.t_nrow, 0);        /* undo scrolling region */
  77.     movecursor(term.t_nrow, 0);             /* In last line.        */
  78.     mlputs("[Starting DCL]\r\n");
  79.     TTflush();                          /* Ignore "ttcol".      */
  80.     sgarbf = TRUE;
  81.     return (sys(NULL, NULL, NULL));        /* NULL => DCL.         */
  82. #endif
  83. #if     CPM
  84.         mlwrite("Not in CP/M-86");
  85. #endif
  86. #if    ST520
  87.     mlwrite("Not in TOS");
  88. #endif
  89. #if     MSDOS & (AZTEC | MSC | TURBO | C86)
  90.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  91.         TTflush();
  92.     TTkclose();
  93.     system("command.com");
  94.     TTkopen();
  95.         sgarbf = TRUE;
  96.         return(TRUE);
  97. #endif
  98. #if     MSDOS & LATTICE
  99.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  100.         TTflush();
  101.     TTkclose();
  102.         sys("\\command.com", "");               /* Run CLI.             */
  103.     TTkopen();
  104.         sgarbf = TRUE;
  105.         return(TRUE);
  106. #endif
  107. #if     V7 | USG | BSD
  108. #if    TERMCAP
  109.     ttscroll(0, term.t_nrow, 0);        /* undo scrolling region */
  110. #endif
  111.         movecursor(term.t_nrow, 0);             /* Seek to last line.   */
  112.         TTflush();
  113.         TTclose();                              /* stty to old settings */
  114.         if ((cp = getenv("SHELL")) != NULL && *cp != '\0')
  115.                 system(cp);
  116.         else
  117. #if    BSD
  118.                 system("exec /bin/csh");
  119. #else
  120.                 system("exec /bin/sh");
  121. #endif
  122.         sgarbf = TRUE;
  123.         sleep(2);
  124.         TTopen();
  125.         return(TRUE);
  126. #endif
  127. }
  128.  
  129. #if    BSD
  130.  
  131. bktoshell()        /* suspend MicroEMACS and wait to wake up */
  132. {
  133.     int pid;
  134.  
  135.     vttidy();
  136.     pid = getpid();
  137.     kill(pid,SIGTSTP);
  138.     return(TRUE);
  139. }
  140.  
  141. rtfrmshell()
  142. {
  143.     TTopen();
  144.     curwp->w_flag = WFHARD;
  145.     sgarbf = TRUE;
  146.     return(TRUE);
  147. }
  148. #endif
  149.  
  150. #if    VMS
  151.  
  152. bktoshell()        /* suspend MicroEMACS and wait to wake up */
  153. {
  154.     int err, code, mypid, ownerpid;
  155.  
  156.     /* don't allow this command if restricted */
  157.     if (restflag)
  158.         return(resterr());
  159.  
  160.     vttidy();
  161.  
  162.     err = 0;
  163.     mypid = 0;
  164.     code = 771;    /* jpi$_owner */
  165.     if (lib$getjpi(&code, &mypid, 0, &ownerpid, 0, 0) != 1) err = 1;
  166.     else if (ownerpid == 0 || mypid == ownerpid) err = 2;
  167.     else if (lib$attach(&ownerpid) != 1) err = 3;
  168.  
  169.     TTopen();
  170.  
  171.     if (err == 0) {
  172.         sgarbf = TRUE;
  173.         curwp->w_flag = WFHARD;
  174.         return(TRUE);
  175.     }
  176.  
  177.     if (err == 1) mlwrite("[Error getting pid]");
  178.     else if (err == 2) mlwrite("[No parent to attach]");
  179.     else mlwrite("[Attach failed]");
  180.     return(FALSE);
  181. }
  182. #endif
  183.  
  184. /*
  185.  * Run a one-liner in a subjob. When the command returns, wait for a single
  186.  * character to be typed, then mark the screen as garbage so a full repaint is
  187.  * done. Bound to "C-X !".
  188.  */
  189. spawn(f, n)
  190. {
  191.         register int    s;
  192.         char            line[NLINE];
  193.  
  194. #if    ST520 & MEGAMAX
  195.     int i,j,k;
  196.     char *sptr,*tptr;
  197. #endif
  198.  
  199. #if     AMIGA
  200.         long newcli;
  201. #endif
  202.  
  203.     /* don't allow this command if restricted */
  204.     if (restflag)
  205.         return(resterr());
  206.  
  207. #if    AMIGA
  208.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  209.                 return (s);
  210.         newcli = Open("CON:0/0/640/200/MicroEMACS Subprocess", NEW);
  211.         Execute(line, 0L, newcli);
  212.         Close(newcli);
  213.         tgetc();     /* Pause.               */
  214.         sgarbf = TRUE;
  215.         return(TRUE);
  216. #endif
  217. #if     ST520 & MEGAMAX
  218.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  219.                 return(s);
  220.     movecursor(term.t_nrow - 1, 0);
  221.     TTclose();
  222. /*
  223.  * break the line into the command and its args
  224.  * be cute about it, if there is no '.' in the filename, try
  225.  * to find .prg, .tos or .ttp in that order
  226.  * in any case check to see that the file exists before we run 
  227.  * amok
  228.  */
  229.     STenv = NULL;
  230.     if((tptr = index(&line[0],' ')) == NULL) { /* no args */
  231.         STcmd = (char *)malloc(strlen(line) + 1);
  232.         strcpy(STcmd,line);
  233.         STargs = NULL;
  234.     }
  235.     else {  /* seperate out the args from the command */
  236.         /* resist the temptation to do ptr arithmetic */
  237.         STcmd = (char *)malloc(strlen(line) + 1);
  238.         for(i = 0,sptr = &line[0]; sptr != tptr; sptr++,i++)
  239.             STcmd[i] = *sptr;
  240.         STcmd[i] = '\0';
  241.         for(; *tptr == ' ' || *tptr == '\t'; tptr++);
  242.         if(*tptr == '\0')
  243.             STargs = NULL;
  244.         else {
  245.             STargs = (char *)malloc(strlen(tptr) + 2);
  246. /* first byte of STargs is the length of the string */
  247.             STargs[0] = strlen(tptr);
  248.             STargs[1] = NULL; /* fake it for strcat */
  249.             strcat(STargs,tptr);
  250.         }
  251.     }
  252. /*
  253.  * before we issue the command look for the '.', if it's not there
  254.  * try adding .prg, .tos and .ttp to see if they exist, if not
  255.  * issue the command as is
  256.  */
  257.     if((tptr = index(STcmd,'.')) == NULL) {
  258.          STwork = (char *)malloc(strlen(STcmd) + 4);
  259.          strcpy(STwork,STcmd);
  260.          strcat(STwork,".prg");
  261.          tptr = index(STwork,'.');
  262.          if(Fsfirst(1,STwork) != 0) { /* try .tos */
  263.              strcpy(tptr,".tos");
  264.              if(Fsfirst(1,STwork) != 0) { /* try .ttp */
  265.                  strcpy(tptr,".ttp");
  266.                  if(Fsfirst(1,STwork) != 0) /* never mind */
  267.                      *STwork = NULL;
  268.                  }
  269.              }
  270.      }
  271.      if(*STwork != NULL)
  272.             Pexec(LOAD_EXEC,STwork,STargs,STenv);         
  273.     else
  274.             Pexec(LOAD_EXEC,STcmd,STargs,STenv);
  275.     TTopen();
  276.         mlputs("\r\n\n[End]");                  /* Pause.               */
  277.         TTgetc();                 /* Pause.               */
  278.         sgarbf = TRUE;
  279.         return (TRUE);
  280. #endif
  281. #if     VMS
  282.         ttscroll(0, term.t_nrow, 0);        /* undo scrolling region */
  283.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  284.                 return (s);
  285.         TTputc('\n');                /* Already have '\r'    */
  286.         TTflush();
  287.         s = sys(line, NULL, NULL);        /* Run the command.     */
  288.     /* if we are interactive, pause here */
  289.     if (clexec == FALSE) {
  290.         mlputs("\r\n\n[End]");        /* Pause.               */
  291.         TTflush();
  292.         tgetc();
  293.         }
  294.         sgarbf = TRUE;
  295.         return (s);
  296. #endif
  297. #if     CPM
  298.         mlwrite("Not in CP/M-86");
  299.         return (FALSE);
  300. #endif
  301. #if     MSDOS | (ST520 & LATTICE)
  302.         if ((s=mlreply("!", line, NLINE)) != TRUE)
  303.                 return(s);
  304.     movecursor(term.t_nrow - 1, 0);
  305.     TTkclose();
  306.         system(line);
  307.     TTkopen();
  308.     /* if we are interactive, pause here */
  309.     if (clexec == FALSE) {
  310.             mlputs("\r\n\n[End]");
  311.             tgetc();
  312.         }
  313.         sgarbf = TRUE;
  314.         return (TRUE);